package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import common.Constants;
import model.CompressorData;
import shuntianfu.com.shuntianfucompressor.CompressorLineFragment;
import shuntianfu.com.shuntianfucompressor.R;

/**
 * Created by haopei on 2016/4/21.
 */
public class YsjDataAdapter extends BaseAdapter {

    private Context context;
    private List<CompressorData> compressorDatas;

    public YsjDataAdapter(Context context, List<CompressorData> compressorDatas) {
        this.context = context;
        this.compressorDatas = compressorDatas;
    }

    @Override
    public int getCount() {
        return compressorDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return compressorDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (null == convertView) {
            holder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_ysjdata_list, null);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tvValue = (TextView) convertView.findViewById(R.id.tv_value);

            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }

        CompressorData compressorData = compressorDatas.get(position);

        if (null != compressorData.getCreateTime()) {
            holder.tvTime.setText(changeTime(Long.valueOf(compressorData.getCreateTime())));
        }
        //默认显示电压数据
        if (CompressorLineFragment.currentSign == Constants.DIAYA) {
            if (null != compressorData.getDianya()) {
                holder.tvValue.setText(compressorData.getDianya());
            }
        } else if (CompressorLineFragment.currentSign == Constants.DIANLIU) {
            if (null != compressorData.getDianliu()) {
                holder.tvValue.setText(compressorData.getDianliu());
            }
        } else if (CompressorLineFragment.currentSign == Constants.WENDU) {
            if (null != compressorData.getWendu()) {
                holder.tvValue.setText(compressorData.getWendu());
            }
        } else if (CompressorLineFragment.currentSign == Constants.GAOYA) {
            if (null != compressorData.getGaoya()) {
                holder.tvValue.setText(compressorData.getGaoya());
            }
        } else if (CompressorLineFragment.currentSign == Constants.DIYA) {
            if (null != compressorData.getDiya()) {
                holder.tvValue.setText(compressorData.getDiya());
            }
        } else {

        }
        return convertView;
    }


    class MyViewHolder {
        TextView tvTime;
        TextView tvValue;
    }

    private String changeTime(long mill) {
        Date date = new Date(mill);
        String strs = "";
        try {
            //yyyy表示年MM表示月dd表示日
            //yyyy-MM-dd是日期的格式，比如2015-12-12如果你要得到2015年12月12日就换成yyyy年MM月dd日
            SimpleDateFormat sdf = new SimpleDateFormat("dd HH:mm:ss");
            //进行格式化
            strs = sdf.format(date);
            System.out.println(strs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strs;
    }
}
